package cn.edu.zucc.JavaChat.ChatServer.Servers;

import java.util.ArrayList;
import java.util.List;

import cn.edu.zucc.JavaChat.ChatServer.ClientHandler;
import cn.edu.zucc.JavaChat.ChatServer.Sender;
import cn.edu.zucc.JavaChat.ChatServer.Component.UserCenter;
import cn.edu.zucc.JavaChat.ChatServer.DB.DAO.DAOFactory;
import cn.edu.zucc.JavaChat.ChatServer.DB.DAO.UserDAO;
import cn.edu.zucc.JavaChat.ChatServer.DB.Entity.User;
import cn.edu.zucc.JavaChat.Request.GetAllFriendRequest;
import cn.edu.zucc.JavaChat.Request.Request;
import cn.edu.zucc.JavaChat.Response.GetAllFriendResponse;

public class GetAllFriendServer implements Server  {
	private UserDAO userDAO = DAOFactory.getUserDAO();
	@Override
	public boolean receiveRequest(Request request, ClientHandler clientHander) {
		GetAllFriendRequest r = (GetAllFriendRequest)request;
		String username = r.getUsername();
		List<User> userList = userDAO.findAllFriend(username);
		List<cn.edu.zucc.JavaChat.Bean.User> userList2 
			= new ArrayList<cn.edu.zucc.JavaChat.Bean.User>();
		
		for(User item : userList){
			cn.edu.zucc.JavaChat.Bean.User item2 
				= new cn.edu.zucc.JavaChat.Bean.User();
			item2.setUsername(item.getUsername());
			item2.setAddress(item.getAddress());
			item2.setBirthday(item.getBirthday());
			item2.setNickname(item.getNickname());
			item2.setRealname(item.getRealname());
			item2.setSex(item.getSex());
			
			item2.setOnline(
					UserCenter.getInstance()
					.getClientHandler(item.getUsername()) != null);
			userList2.add(item2);
		}
		GetAllFriendResponse response = new GetAllFriendResponse();
		response.setToUsername(username);
		response.setUserList(userList2);
		Sender.sendResponse(response);
		return true;
	}

}
